//
//  Karte.m
//  Lextra Junior ueb IT
//
//  Created by Dirk Lieder on 04.08.11.
//  Copyright 2011 Phosphon. All rights reserved.
//

#import "Karte.h"
#import <QuartzCore/QuartzCore.h>
#import "getFilenameHD.h"

#import "Spiel4ViewController.h"

@implementation Karte

@synthesize front,back;
@synthesize DelegateView;
@synthesize imageView;

- (id)initWithElement: (NSDictionary *) element
            
                media: (BOOL) ton
                speed: (CGPoint) sxy 
                  pos: (CGPoint) pxy
             Delegate: (id)new_delegate
            DelegateView: (UIView*) view
{
    self = [super init];
    if (self) {
        info = element;
        media = ton;
        speed = sxy;
        pos = pxy;
        Delegate = new_delegate;
        DelegateView = view;
        
        callback = false;
        callback2 = false;
        
        // get pngs
        if(ton){
             front = [[UIImage imageNamed:@"karte_ton"] retain];
        } else {
            
            NSString *myName = [info objectForKey:@"bild"];
            NSString *substring = [PHOSPHON imagePost:myName imagePostfix:@"_k"];
            front = [[UIImage imageNamed: substring] retain];
        }
//___NGUYEN_HUY_SON__
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
        {
            back = [[UIImage imageNamed:@"karte_rueck@1.5x.png"] retain]; 
        }
        else
        {        
            back = [[UIImage imageNamed:@"karte_rueck"] retain]; 
        }        
   //END_NGUYEN_HUY_SON     
        imageView = [ [ UIImageView alloc ] initWithImage:back ];
        [DelegateView addSubview:imageView];
        
        CGAffineTransform rotate = CGAffineTransformMakeScale( 1,1 );
        [imageView setTransform:rotate];  
        //_-_NGUYEN_HUY_SON___
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
        {
            imageView.center = CGPointMake(150/2+pos.x, 150/2+pos.y);
        }
        else
        {
            imageView.center = CGPointMake(90/2+pos.x, 90/2+pos.y);
        }
        //___NGUYEN_HUY_SON_END
        imageView.layer.shadowColor = [UIColor blackColor].CGColor;
        imageView.layer.shadowOpacity = 0.5;
        imageView.layer.shadowRadius = 1;
        imageView.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);

        animationRunning = false;
        animationPosition = 10.0;
        animationPhase = animationTarget = 10;
    }
    return self;
}
//____NGUYEN_HUY_SON___
-(void) setPos: (CGPoint) pxy
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        imageView.frame = CGRectMake(pxy.x, pxy.y, 150, 150);
        pos = pxy;
    }
}

- (void) showCard {    
    animationRunning = true;
    animationTarget = 20;
}

- (void) showCardCallback { 
    if(animationTarget != 20){
        animationRunning = true;
        animationTarget = 20;
        callback = true;
    } else {
        [(Spiel4ViewController*)Delegate callback];
    }
}

- (void) hideCard{    
    animationRunning = true;
    animationTarget = 10;
}
- (void) delite{
    animationRunning = true;
    animationTarget = 15;
}

- (void) renderCard {
    
    float anispeed = 0.66;
    float outPos = 0.0;
 
    if(animationRunning){
        switch (animationTarget) {
            case 10:
                animationPosition -= anispeed;
                if(animationPosition < 10){
                    animationPosition = 10;
                    animationPhase = animationPosition;
                }
                break;
 
            case 20:
                animationPosition += anispeed;
                if(animationPosition >= 20){
                    animationPosition = 20;
                    if(callback) 
                        animationTarget = 50;
                    else
                        animationPhase = animationPosition;
                }
                break;
                
            case 50:
                animationPosition += anispeed;
                if(animationPosition >= 50){
                    animationPosition = animationTarget = animationPhase = 20;
                    callback2 = true;
                    callback = false;
                }
                break;

            case 15:
                if(animationPosition>15){
                    animationPosition -= anispeed;
                    if(animationPosition <15) animationPosition = 15;
                } else {
                    animationPosition += anispeed;
                    if(animationPosition >15) animationPosition = 15;
                }
                break;
        }
        
        if(animationPosition == 15 && animationTarget == 15){
            imageView.hidden = true;
        } else {
            if(animationPosition<=20){
                outPos = (animationPosition-10.0)/5.0-1;
                if(outPos>=0)
                    imageView.image = front;
                else 
                    imageView.image = back;
            
                CGAffineTransform rotate = CGAffineTransformMakeScale( fabs(outPos),1 );
                [imageView setTransform:rotate];  
            }
        }
        if(animationPhase == animationPosition) {
            animationRunning = false;
        }
        if( callback2){
            callback2 = false;
            [Delegate callback];
        }
           
    }
}


- (void) remove{
    [imageView removeFromSuperview];
}


-(void) dealloc {
    [super dealloc];
}

@end
